﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

using System.Web;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

namespace mmoss.application.classes
{
    class s3upload
    {
        static string accessKey = System.Configuration.ConfigurationManager.AppSettings.Get("AWSAccessKey");
        static string  secretKey = System.Configuration.ConfigurationManager.AppSettings.Get("AWSSecretKey");
        public static void Upload(string l_filePath, int l_mmcID)
        {
            //load file as stream and hit core upload method
            //Stream l_fileStream = new Stream
            FileStream l_fileStream = new FileStream(l_filePath, FileMode.Open, FileAccess.Read);
            Upload(l_fileStream, l_mmcID);
        }
        public static bool bucketExists(string bucketName)
        {
            return true;
        }
        public static void  createBucket(string bucketName)
        {
            /*
            ThreeSharpConfig config;
            IThreeSharp service;
            String basePath;
        
            Thread statsThread;
            bool isRunning = true;


            //configure
            basePath = "C:\\";

            config = new ThreeSharpConfig();
            //config.AwsAccessKeyID = Properties.Settings.Default.AwsAccessKeyID;
            //config.AwsSecretAccessKey = Properties.Settings.Default.AwsSecretAccessKey;
            config.AwsAccessKeyID = accessKey;
            config.AwsSecretAccessKey = secretKey;
            config.ConnectionLimit = 40;
            config.IsSecure = true;

            // It is necessary to use the SUBDOMAIN CallingFormat for accessing EU buckets
            config.Format = CallingFormat.SUBDOMAIN;

            service = new ThreeSharpQuery(config);



            using (BucketAddRequest request = new BucketAddRequest(bucketName))
            {
                

                StringBuilder sbBucketConfig = new StringBuilder();
                sbBucketConfig.AppendLine("<CreateBucketConfiguration>");
                sbBucketConfig.AppendLine("<LocationConstraint>" + bucketName + "</LocationConstraint>");
                sbBucketConfig.AppendLine("</CreateBucketConfiguration>");
                request.LoadStreamWithString(sbBucketConfig.ToString());

                using (BucketAddResponse response = service.BucketAdd(request))
                { }

                //BindBucketNames();
                //this.comboBoxBucketNames.SelectedItem = this.textBoxBucketName.Text.ToLower().Trim().Replace(' ', '_');
             * 
            }
            */
        }
        public static void Upload(Stream l_file, int l_mmcID)
        {
            string bucketName = "mmc_" + l_mmcID.ToString();
            
            AmazonS3 client;
            using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
            {
                MemoryStream ms = new MemoryStream();
                try
                {
                    //createBucket(bucketName);
                    PutObjectRequest request = new PutObjectRequest();
                    request.WithBucketName(bucketName)
                    .WithCannedACL(S3CannedACL.PublicRead)
                    .WithKey("patient/patient123.jpg").InputStream = l_file;
                    Amazon.S3.Model.S3Response response = client.PutObject(request);
                }
                catch (AmazonS3Exception ex)
                {
                    //put in handling logic here
                }
            }

            /*
            using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID))
            {
                // simple object put
                PutObjectRequest request = new PutObjectRequest();
                request.WithContentBody("this is a test")
                    .WithBucketName(bucketName)
                    .WithKey(keyName);


                S3Response response = client.PutObject(request);
                response.Dispose();

                // put a more complex object with some metadata and http headers.
                PutObjectRequest titledRequest = new PutObjectRequest();
                titledRequest.WithMetaData("title", "the title")
                    .WithContentBody("this object has a title")
                    .WithBucketName(bucketName)
                    .WithKey(keyName);

                using (S3Response responseWithMetadata = client.PutObject(request))
                {
                    WebHeaderCollection headers = response.Headers;
                    foreach (string key in headers.Keys)
                    {
                        Console.WriteLine("Response Header: {0}, Value: {1}", key, headers.Get(key));
                    }
                }
            }
             * */
        }
    }
}
